<?php
/**
 * 文件处理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\WqAttachmentMod;

class File extends Action
{
    private $WqAttachment = null;
	public function __construct()
	{
		parent::__construct();
	}

    /**
     * 系统通用上传文件，远程或者本地
     *
     */
	public function upload()
    {
        load()->func('file');
        $_result = file_upload($_FILES['file'],'image');
        $_success = true;
        if($_result['success']){
            if($this->_w['setting']['remote']['type'] != 0){
                if(!file_remote_upload($_result['path'])) $_success = false;
            }
        }else{
            $_success = false;
        }
        if($_success){
            $this->_result = array('status'=>'success','path'=>$_result['path'],'url'=>tomedia($_result['path']));
        }
        $this->result($this->_result);
    }

    /**
     * 通用删除文件
     * @param path 文件路径
     */
    public function remove()
    {
        load()->func('file');
        file_delete($this->_gpc['path']);
        file_remote_delete($this->_gpc['path']);
        $this->_result = array('status'=>'success');
        $this->result($this->_result);
    }

	/**
	 * 上传编辑器图片
	 */
	public function uploadImage()
	{
        load()->func('file');
        $_result = file_upload($_FILES['file'],'image');
        if($_result['success']){
            $this->_result = array('status'=>'success','data'=>$_result['path']);
        }
        $this->result($this->_result);
	}

    /**
     * 获取图片的真是地址src
     * @param path 文件目录路径
     */
	public function getSrc()
    {
        $this->_result = array('status'=>'success','data'=>tomedia($this->_gpc['path']));
        $this->result($this->_result);
    }

    /**
     * 获取当前公众号系统图片列表总数
     */
    public function getImgTotal()
    {
        $this->WqAttachment = new WqAttachmentMod();
        $this->_result = array('status'=>'success','data'=>abs($this->WqAttachment->total(array('uniacid'=>$this->_uniacid,'type'=>1))));
        $this->result($this->_result);
    }

    /**
     * 获取当前公众号系统图片列表
     */
    public function getImgList()
    {
        $this->WqAttachment = new WqAttachmentMod();
        $_list = $this->WqAttachment->findAll(array('uniacid'=>$this->_uniacid,'type'=>1),array('order'=>array('createtime','desc'),'limit'=>$this->_gpc['limit']),array('id','filename','attachment'));
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_list[$_k]['src'] = tomedia($_v['attachment']);
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 上传图片至公众号系统
     * @param FILES['file']  上传的文件
     */
    public function uploadImg()
    {
        load()->func('file');
        $_result = file_upload($_FILES['file'],'image');
        if($_result['success']){
            if($this->_w['setting']['remote']['type'] != 0){
                file_remote_upload($_result['path']);
            }
            if($this->uploadImgRecord($_result['path'])){
                $this->_result = array('status'=>'success');
            }
        }
       $this->result($this->_result);
    }

    /**
     * 数据库新增上传图片记录
     * @param $_attachment 上传的图片路径
     * @return Boolen 是否新增成功
     */
    private function uploadImgRecord($_attachment)
    {
        $this->WqAttachment = new WqAttachmentMod();
        return $this->WqAttachment->insert(array(
            'uniacid'=>$this->_uniacid,
            'uid'=>1,
            'filename'=>$_FILES['file']['name'],
            'attachment'=>$_attachment,
            'type'=>1,
            'createtime'=>TIMESTAMP,
            'group_id'=>-1
        ));
    }

    /**
     * 删除微擎系统图片
     * @param attachment_id  删除的id
     */
    public function deleteImg()
    {
        load()->func('file');
        $this->WqAttachment = new WqAttachmentMod();
        $_attachment = $this->WqAttachment->findOne(array('id'=>$this->_gpc['attachment_id']),array('attachment'));
        if($_attachment) {
            if (is_file(ATTACHMENT_ROOT . $_attachment['attachment'])) {
                $_result = file_delete($_attachment['attachment']);
            } else {
                $_result = file_remote_delete($_attachment['attachment']);
            }
            if($_result){
                if($this->WqAttachment->delete(array('id'=>$this->_gpc['attachment_id']))){
                    $this->_result = array('status'=>'success');
                }
            }
        }
        $this->result($this->_result);
    }

}